﻿using System;
using System.Collections.Generic;

namespace SynergyCounts
{
    [Serializable]
    public sealed class Condition
    {
        public UInt64 sourceLinkID { get; set; }
        public UInt64 conditionID { get; set; }
        public String conditionType { get; set; }
        public UInt64 destinationLinkID { get; set; }
        public UInt32 conditionSourceCode { get; set; }
        public String vehicleAccess { get; set; }

        public Condition()
        {

        }

        public Condition(UInt64 sourceLinkID, UInt64 conditionID, String conditionType, UInt64 destinationLinkID, UInt32 conditionSourceCode, String vehicleAccess)
        {
            this.sourceLinkID = sourceLinkID;
            this.conditionID = conditionID;
            this.conditionType = conditionType;
            this.destinationLinkID = destinationLinkID;
            this.conditionSourceCode = conditionSourceCode;
            this.vehicleAccess = vehicleAccess;
        }

        public static IReadOnlyList<Condition> ConditionParser(String conditions)
        {
            List<Condition> conditionList = new List<Condition>();

            Char[] rowSplitter = { '\r', '\n' };
            Char[] columnSplitter = { '\t', ';', '|' };

            String[] conditionsArray = conditions.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);
            foreach (String condition in conditionsArray)
            {
                 String[] conditionData = condition.Split(columnSplitter, StringSplitOptions.None);

                 UInt64 sourceLinkID;
                 UInt64.TryParse(conditionData[1], out sourceLinkID);
                 UInt64 conditionID;
                 UInt64.TryParse(conditionData[2], out conditionID);
                 String conditionType = conditionData[3];
                 UInt64 destinationLinkID;
                 UInt64.TryParse(conditionData[4], out destinationLinkID);
                 UInt32 conditionSourceCode;
                 UInt32.TryParse(conditionData[5], out conditionSourceCode);
                 String vehicleAccess = conditionData[6];
                 
                conditionList.Add(new Condition(sourceLinkID, conditionID, conditionType, destinationLinkID, conditionSourceCode, vehicleAccess));
            }
            return conditionList;
        }
    }
}
